package com.by.yz.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.GridView;

import com.by.yz.R;
import com.by.yz.activity.base.BaseActivity;
import com.by.yz.adapter.GridAdapter;
import com.by.yz.entity.BingMing;
import com.by.yz.loginUtil.DiagnosisRESTFul;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lp on 2017/4/13.
 */
public class diagnosis_bingming extends BaseActivity{
    private String text;
    private GridView gv;
    private GridAdapter adapter;
    private List<Map<String,Object>> list;
    private static int bmNum;
    private static List<String> bmName =new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diagnosis_bingming);
        setToolbarTitle("选择病名");
        initView();
        initDatas();

    }

    private void initDatas() {

    }

    private void initView() {
        gv = (GridView) findViewById(R.id.BingMing);
        list = new ArrayList<Map<String,Object>>();

        DiagnosisRESTFul bmREST=new DiagnosisRESTFul();
        bmREST.AllBingMing(new DiagnosisRESTFul.MyGetCallBack(){
            @Override
            public void ok(JSONArray response) throws JSONException {
                bmNum = response.length();
                for (int i = 0; i < bmNum; i++) {
                    BingMing bm;
                    JSONObject jsonObject = response.getJSONObject(i);
                    bm = ToJSonObject.jsonToBean(jsonObject.toString(), BingMing.class);
                    bmName.add(i,bm.getBmName());
                }
                gridList();
            }
            @Override
            public void error(String error) {
                Log.e("error",error);
            }
        });
    }

    void gridList(){
        Bundle b = new Bundle();
        try{Map<String,Object> map = null;
            for(int i=0;i<bmNum;i++){
                map = new HashMap<String,Object>();
                map.put("text", bmName.get(i));
                map.put("Type", 2);
                list.add(map);
            }
            b.putBoolean("bindGrid", true);
        }catch(Exception e){}finally{
            Message msg = handler.obtainMessage();
            msg.setData(b);
            handler.sendMessage(msg);
        }
    }

    private void bindGrid(){
        adapter = new GridAdapter(this, list);
        gv.setAdapter(adapter);
    }

    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            try{
                if(msg.getData().containsKey("bindGrid")){
                    bindGrid();
                }
            }catch(Exception e){}
        }
    };

}
